home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / os2 / bind493a.zip / res / getnetent.c < prev    next >
C/C++ Source or Header  |  1996-06-08  |  5KB  |  166 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *      This product includes software developed by the University of
  16.  *      California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
  35.  *      Dep. Matematica Universidade de Coimbra, Portugal, Europe
  36.  *
  37.  * Permission to use, copy, modify, and distribute this software for any
  38.  * purpose with or without fee is hereby granted, provided that the above
  39.  * copyright notice and this permission notice appear in all copies.
  40.  *
  41.  * from getnetent.c     1.1 (Coimbra) 93/06/02
  42.  */
  43.  
  44. #if defined(LIBC_SCCS) && !defined(lint)
  45. static char sccsid[] = "@(#)getnetent.c 8.1 (Berkeley) 6/4/93";
  46. static char rcsid[] = "$Id: getnetent.c,v 8.1 1994/12/15 06:24:24 vixie Exp $";
  47. #endif /* LIBC_SCCS and not lint */
  48.  
  49. #include <sys/param.h>
  50. #include <sys/socket.h>
  51. #include <netinet/in.h>
  52. #include <arpa/inet.h>
  53. #include <arpa/nameser.h>
  54.  
  55. #include <stdio.h>
  56. #include <resolv.h>
  57. #include <netdb.h>
  58. #include <string.h>
  59.  
  60. #ifndef _PATH_NETWORKS
  61. #ifdef __EMX__
  62. #define _PATH_NETWORKS  "/tcpip/etc/networks"
  63. #else
  64. #define _PATH_NETWORKS  "/etc/networks"
  65. #endif
  66. #endif
  67.  
  68. #define MAXALIASES      35
  69.  
  70. static FILE *netf;
  71. static char line[BUFSIZ+1];
  72. static struct netent net;
  73. static char *net_aliases[MAXALIASES];
  74. int _net_stayopen;
  75.  
  76. void _setnetent __P((int));
  77. void _endnetent __P((void));
  78.  
  79. void
  80. setnetent(stayopen)
  81.         int stayopen;
  82. {
  83.  
  84.         sethostent(stayopen);
  85.         _setnetent(stayopen);
  86. }
  87.  
  88. void
  89. endnetent()
  90. {
  91.  
  92.         endhostent();
  93.         _endnetent();
  94. }
  95.  
  96. void
  97. _setnetent(f)
  98.         int f;
  99. {
  100.  
  101.         if (netf == NULL)
  102.                 netf = fopen(_PATH_NETWORKS, "r" );
  103.         else
  104.                 rewind(netf);
  105.         _net_stayopen |= f;
  106. }
  107.  
  108. void
  109. _endnetent()
  110. {
  111.  
  112.         if (netf) {
  113.                 fclose(netf);
  114.                 netf = NULL;
  115.         }
  116.         _net_stayopen = 0;
  117. }
  118.  
  119. struct netent *
  120. getnetent()
  121. {
  122.         char *p;
  123.         register char *cp, **q;
  124.  
  125.         if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL)
  126.                 return (NULL);
  127. again:
  128.         p = fgets(line, BUFSIZ, netf);
  129.         if (p == NULL)
  130.                 return (NULL);
  131.         if (*p == '#')
  132.                 goto again;
  133.         cp = strpbrk(p, "#\n");
  134.         if (cp == NULL)
  135.                 goto again;
  136.         *cp = '\0';
  137.         net.n_name = p;
  138.         cp = strpbrk(p, " \t");
  139.         if (cp == NULL)
  140.                 goto again;
  141.         *cp++ = '\0';
  142.         while (*cp == ' ' || *cp == '\t')
  143.                 cp++;
  144.         p = strpbrk(cp, " \t");
  145.         if (p != NULL)
  146.                 *p++ = '\0';
  147.         net.n_net = inet_network(cp);
  148.         net.n_addrtype = AF_INET;
  149.         q = net.n_aliases = net_aliases;
  150.         if (p != NULL)
  151.                 cp = p;
  152.         while (cp && *cp) {
  153.                 if (*cp == ' ' || *cp == '\t') {
  154.                         cp++;
  155.                         continue;
  156.                 }
  157.                 if (q < &net_aliases[MAXALIASES - 1])
  158.                         *q++ = cp;
  159.                 cp = strpbrk(cp, " \t");
  160.                 if (cp != NULL)
  161.                         *cp++ = '\0';
  162.         }
  163.         *q = NULL;
  164.         return (&net);
  165. }
  166.